package controller;

import com.google.common.collect.ImmutableMap;
import common.pojo.CommonResult;
import common.utils.JsonUtils;
import httpclient.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.UserShopInfo;
import pojo.form.ChatListAddForm;
import pojo.form.ChatListQueryForm;
import service.ChatListService;
import service.cfg.ShopCfg;

/**
 * Created by asus on 2018/11/22.
 */
@RequestMapping("/chat")
@Controller
public class ChatListController {
    @Autowired
    ChatListService chatListService;

    @Autowired
    ShopCfg shopCfg;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addChatList(
            @ModelAttribute ChatListAddForm addForm
    ) {
        return CommonResult.returnDataWrapper(chatListService.addCharList(addForm));
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getChatList(
            @ModelAttribute ChatListQueryForm queryForm,
            @RequestParam(value = "start", required = false) String start,
            @RequestParam(value = "limit", required = false) String limit,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "50") String pageSize
    ) {
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }
        return CommonResult.returnDataWrapper(chatListService.getChatList(queryForm, NumberUtils.toInt(curPage), NumberUtils.toInt(pageSize)));
    }


    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getChatPage(@ModelAttribute ChatListQueryForm queryForm,
                                    @RequestParam(value = "start", required = false) String start,
                                    @RequestParam(value = "limit", required = false) String limit,
                                    @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "50") String pageSize
    ) {
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }

        if (StringUtils.isBlank(queryForm.getWxId())) {
            return CommonResult.returnFailsWrapper("请传入wxId");
        }
        CommonResult commonResult = HttpClientUtils.get(shopCfg.getShopInfo(), ImmutableMap.of("wxId", queryForm.getWxId()));
        UserShopInfo shopInfo = JsonUtils.objectToPojo(commonResult.getData(), UserShopInfo.class);
        if(shopInfo==null){
            return CommonResult.returnFailsWrapper("此店主信息不存在");
        }
        queryForm.setShopId(shopInfo.getId());
        return CommonResult.returnDataWrapper(chatListService.getChatList(queryForm, NumberUtils.toInt(curPage), NumberUtils.toInt(pageSize)));
    }
}
